<?php


require_once(dirname(__FILE__).'/../../common/log.php');
require_once(dirname(__FILE__).'/jpush/autoload.php');


use JPush\Client as JPush;


function jpush($registration_id, $title, $content, $flag = true)
{
    $app_key        = '1afec646e4e5e628ce98becc';
    $master_secret  = '541a615fe3bb90aab527150c';
    $client         = new JPush($app_key, $master_secret);

    try
    {
        $response  = $client->push()
            ->setPlatform(array('ios', 'android'))
            ->addRegistrationId($registration_id)
            ->setNotificationAlert($content)
            ->iosNotification($title)
            ->androidNotification($content, array('title' => $title))
            ->options(array(
                // apns_production: 表示APNs是否生产环境，
                // True 表示推送生产环境，False 表示要推送开发环境；如果不指定则默认为推送开发环境

                'apns_production' => $flag,
                'third_party_channel' => ['huawei' => ['skip_quota' => false, 'importance' => 'HIGH', 'target_user_type' => 1]],
            ))
            ->send();
    }
    catch (\JPush\Exceptions\APIConnectionException $e)
    {
        loge($e->getMessage());

        return -1;
    }
    catch (\JPush\Exceptions\APIRequestException $e)
    {
        loge($e->getMessage());

        return -1;
    }

    return 0;
}

